<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class WorksController extends BaseController {
    public function index(){
        $this->display();
    }
    // 作品上传
    public function workUploads(){
        $this->init();
        $this->display();
    }
    // 作品提交
    public function workSave(){
        // var_dump($_POST);
        $works=M('works');

        $data['title'] = I('post.title');
        $data['content'] = I('post.work_edit');
        $data['type'] = I('post.type');
        $data['time'] = time();
        $config = array(
            'maxSize'    =>    3145728,
            'savePath'   =>    '/works_img/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    false,
            'subName'    =>    array('date','Ymd'),
            
        );
        $upload = new \Think\Upload($config);
        if(!empty($_FILES['works_img']['tmp_name'])){
            $info = $upload->uploadOne($_FILES['works_img']);
         $data['main_img'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
        // 文件提交
         $config = array(
            'maxSize'    =>    50000000,
            'savePath'   =>    '/works_file/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('zip', 'rar', 'cab', 'arj','7-zip','tar','gzip','uue','bz2','jar'),
            'autoSub'    =>    false,
            'subName'    =>    array('date','Ymd'),
            
        );
        $upload = new \Think\Upload($config);
        if(!empty($_FILES['works_file']['tmp_name'])){
            $info = $upload->uploadOne($_FILES['works_file']);
            $data['file'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
//      var_dump($_FILES);
        
        $workSave=$works->add($data);
        if($workSave){
            $this->success("提交成功",U("Works/workList"));
        }else{
            $this->error('提交失败');
        }
    }
        // $a =$article->where('id=2')->find();
        // $a['content']=html_entity_decode($a['content']);
        // $this->assign('data',$a);
        // $this->success('提交成功','articleList');
        
        
       
//      var_dump($_FILES);
        

    // 作品列表
    public function workList(){
        $this->init();
        $works=M('works');
        $workList =$works->select();
        for($i=0;$i<count($workList);$i++){
            $titleList[$i]=$workList[$i]['title']; 
        }
        $this->assign('workList',$workList);
        // var_dump($titleList);
        $this->display();
    }
    // 删除作品
    public function deleteWork(){
        $works=M('works');
        $deleteWork=$works->where('id='.$_GET['id'])->delete();
        if($deleteWork){
            $this->success("删除成功",U("Works/workList"));
        }else{
            $this->error('删除失败');
        }
        // $this->redirect('articleList', 5, '页面跳转中...');
    }
    // 修改作品
    public function reviseWorks(){
        $this->init();
        
        $works=M('works');
        $reviseWorks=$works->where('id='.$_GET['id'])->select();
        $reviseWorks[0]['content']=html_entity_decode($reviseWorks[0]['content']);
        $this->assign('reviseWorks',$reviseWorks);
        $this->display();
    }
    //作品修改保存
    public function reviseWorksSave(){
        $works=M('works');

        $data['title'] = I('post.title');
        $data['content'] = I('post.work_edit');
        $data['type'] = I('post.type');
        $data['recommended']=I('post.recommended');
        $data['time'] = time();
        $config = array(
            'maxSize'    =>    3145728,
            'savePath'   =>    '/works_img/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    false,
            'subName'    =>    array('date','Ymd'),
            'replace'    =>    true,
        );
        $upload = new \Think\Upload($config);
        if(!empty($_FILES['works_img']['tmp_name'])){
            $info = $upload->uploadOne($_FILES['works_img']);
            $data['main_img'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
        $config = array(
            'maxSize'    =>    50000000,
            'savePath'   =>    '/works_file/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('zip', 'rar', 'cab', 'arj','7-zip','tar','gzip','uue','bz2','jar'),
            'autoSub'    =>    false,
            'subName'    =>    array('date','Ymd'),
            
        );
        $upload = new \Think\Upload($config);
        if(!empty($_FILES['works_file']['tmp_name'])){
            $info = $upload->uploadOne($_FILES['works_file']);
            $data['file'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
//      var_dump($_FILES);
        $reviseWorksSave=$works->where('id='.$_GET['id'])->save($data);
        if($reviseWorksSave){
            $this->success("修改成功",U("Works/workList"));
        }else{
            $this->error('修改失败');
        }
    }
}